package com.studentmanagement.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.studentmanagement.dao.UserMapper;
import com.studentmanagement.model.PageBean;
import com.studentmanagement.model.User;
import com.studentmanagement.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean addUser(User user) {
        return userMapper.insertUser(user) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean deleteUser(Integer userId) {
        return userMapper.deleteUser(userId) > 0;
    }

    @Override
    @Transactional(readOnly = true)
    public User getUserById(Integer userId) {
        return userMapper.selectUserById(userId);
    }

    @Override
    @Transactional(readOnly = true)
    public User getUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    @Transactional(readOnly = true)
    public User validateUser(String username, String password) {
        return userMapper.selectUserByUsernameAndPassword(username, password);
    }

    @Override
    @Transactional(readOnly = true)
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    @Transactional(readOnly = true)
    public PageBean<User> getUsersByPage(int pageNum, int pageSize, String sortField, String sortOrder) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 设置排序参数
        Map<String, Object> params = new HashMap<>();
        params.put("sortField", sortField);
        params.put("sortOrder", sortOrder);

        // 查询数据
        List<User> users = userMapper.selectUsersByPage(params);

        // 获取分页信息
        PageInfo<User> pageInfo = new PageInfo<>(users);

        // 封装分页结果
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageNum(pageInfo.getPageNum());
        pageBean.setPageSize(pageInfo.getPageSize());
        pageBean.setTotalRecord((int) pageInfo.getTotal());
        pageBean.setList(pageInfo.getList());
        pageBean.setSortField(sortField);
        pageBean.setSortOrder(sortOrder);

        return pageBean;
    }

    @Override
    public boolean registerUser(String username, String password, String role) {
        // 检查用户名是否已存在
        User existingUser = userMapper.selectUserByUsername(username);
        if (existingUser != null) {
            return false;
        }

        // 创建新用户
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);

        return userMapper.insertUser(user) > 0;
    }
}